package org.zhs.springframework.factory.support;

import org.zhs.springframework.BeansException;
import org.zhs.springframework.factory.BeanFactory;
import org.zhs.springframework.factory.config.BeanDefinition;

/**
 * 抽象Bean工厂，定义模板方法
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) throws BeansException {
        Object bean = getSingleton(beanName);
        if(bean != null){
            return bean;
        }

        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName);

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition);

}
